﻿
$(window).load(function () {
    $.ploadFile("layer", function (script, textStatus) {
    });

    $("#J_navbar li").hover(function () {
        if ($(this).hasClass("last")) {
            $("#J_navbar .line").css({ left: '', right: 0 + 'px' });
        } else {
            $("#J_navbar .line").animate({ left: 109 * ($(this).index()) + 'px', right: '' }, 100);
        }
    }, function () {
    })
    .click(function () {
        $("#J_navbar .selected").removeClass("selected");
        $(this).addClass("selected");

        if ($(this).attr("data-spm") == "待付款") {
            cSetCtrlV("dplTRADE_STATUS_TXT", "等待买家付款");
            cSetCtrlV("dplREVIEW_STATUS_TXT", "");
            cSetCtrlV("dplAFTER_SVC_STATUS_TXT", "");
        }
        else if ($(this).attr("data-spm") == "待发货") {
            cSetCtrlV("dplTRADE_STATUS_TXT", "买家已付款");
            cSetCtrlV("dplREVIEW_STATUS_TXT", "");
            cSetCtrlV("dplAFTER_SVC_STATUS_TXT", "");
        }
        else if ($(this).attr("data-spm") == "待收货") {
            cSetCtrlV("dplTRADE_STATUS_TXT", "卖家已发货");
            cSetCtrlV("dplREVIEW_STATUS_TXT", "");
            cSetCtrlV("dplAFTER_SVC_STATUS_TXT", "");
        }
        else if ($(this).attr("data-spm") == "退款中") {
            cSetCtrlV("dplTRADE_STATUS_TXT", "退款中的订单");
            cSetCtrlV("dplREVIEW_STATUS_TXT", "");
            cSetCtrlV("dplAFTER_SVC_STATUS_TXT", "");
        }
        else if ($(this).attr("data-spm") == "待评价") {
            cSetCtrlV("dplTRADE_STATUS_TXT", "");
            cSetCtrlV("dplREVIEW_STATUS_TXT", "需买家评价");
            cSetCtrlV("dplAFTER_SVC_STATUS_TXT", "");
        } else {
            cSetCtrlV("dplTRADE_STATUS_TXT", "");
            cSetCtrlV("dplREVIEW_STATUS_TXT", "");
            cSetCtrlV("dplAFTER_SVC_STATUS_TXT", "");
        }
        doToPageNm();
    });
    $("#J_navbar").hover(function () { }, function () {
        if ($($("#J_navbar .selected")).hasClass("last")) {
            $("#J_navbar .line").css({ left: '', right: 0 + 'px' });
        } else {
            $("#J_navbar .line").animate({ left: 109 * ($("#J_navbar .selected").index()) + 'px' }, 100);
        }
    });
    $("#J_SearchBoxToggle").click(function () {
        if ($(".searchbar-expanded").length <= 0) {
            $("#J_MoreControl").show();
            $("#J_QueryConditionForm").addClass("searchbar-expanded");
            $("#J_SearchBox_Submmit").show();
        } else {
            $("#J_MoreControl").hide();
            $("#J_QueryConditionForm").removeClass("searchbar-expanded");
        }
    });
    $("#J_SearchBox_Submmit").click(function () {
        doToPageNm();
    });
    $("#J_QueryConditionForm .search-btn button").click(function () {
        doToPageNm();
    });

    $("#J_JumpTo").click(function () {
        var $pinpNum = $(this).parent().find(" input");
        if (!$pinpNum.val()) { return; }
        if ($pinpNum.val() > $pinpNum.attr("max")) {
            doToPageNm($pinpNum.attr("max"));
        } else {
            doToPageNm($pinpNum.val());
        }
    });
    $("#J_BoughtTable .J_AllSelector").click(function () {
        $("#J_BoughtTable .J_Selector").attr("checked", $(this).attr("checked") == true || $(this).attr("checked") == "checked");
    });

    $("#J_BoughtTable .J_CombinPay").click(function () {
        doToPay();
    });
    $("#J_BoughtTable .J_BatchReceive").click(function () {
        doToConfirmReceipt();
    });
});
$(function () {
    $("#J_BoughtTable tbody").live("mouseover", function () { $(this).addClass("hover"); }).live("mouseout", function () { $(this).removeClass("hover"); });

    $("#address-list li").live("click", function () {
        var $self = $(this);
        $self.prevAll().removeClass("selected");
        $self.nextAll().removeClass("selected");
        $self.addClass("selected").find(" input").attr("checked", true);
        updateorderaddr();
    });
    $(".J_Modify").live("click", function () {
        ShowAddress($("#address-list input[checked='checked']").val());
    });

});

function doToPay(_orderid) {
    if (!_orderid || _orderid == '') { 
        _orderid = '';
        $("#J_BoughtTable .J_Selector").each(function () {
            if ($(this).attr("checked") == true || $(this).attr("checked") == "checked") {
                _orderid += _orderid == '' ? $(this).val() : ',' + $(this).val();
            }
        });
    }
    if (!_orderid || _orderid == '') {alert("请先选择订单！");return; }
    window.location.href = '/c_PayCashier.htm?ID=' + _orderid;
};

function doToConfirmReceipt(_orderid) {
    if (!_orderid || _orderid == '') { 
        _orderid = '';
        $("#J_BoughtTable .J_Selector").each(function () {
            if ($(this).attr("checked") == true || $(this).attr("checked") == "checked") {
                _orderid += _orderid == '' ? $(this).val() : ',' + $(this).val();
            }
        });
    }
    if (!_orderid || _orderid == '') { alert("请先选择订单！"); return; }
    window.location.href = '/c_ConfirmReceipt.htm?ID=' + _orderid;
};

function doToPageNm(_pNum) {
    var loadi = layer.load('正在查询中…');
    try {
        function closeload() { layer.close(loadi); }
        var _jsParam = new jsonRow();
        GetFormJson("J_MoreControl", _jsParam);
        _jsParam.AddCell("ACTION", "doFinderBoughtList");
        _jsParam.AddCell("NEW_PAGE_NUM", _pNum || $("#datacurrpage").val());
        _jsParam.AddCell("PAGE_ROW_COUNT", "20");
        _jsParam.AddCell("PAGE_ROW_SORT", "INITIAL_DT DESC");

        _jsParam.AddCell("GOODS_NME", $("#J_BaobeiName").val());

        ajaxComm(_jsParam, null, closeload);
    }
    catch (err) {
        layer.close(loadi);
    }
};

function updateTableHtml(_data) {
    var tpl = HtmlUtil.htmlDecode(document.getElementById('list_temp').innerHTML);
    laytpl(tpl).render(_data, function (render) {
        $("#J_BoughtTable").find(" tbody").remove(); $("#J_BoughtTable").find(" thead").after(render);
    });
    $("#datacurrpage").val(_data.page);
    createPaging('J_trade_pagenation', _data.page, _data.total, function (_topagenum) { doToPageNm(_topagenum); });
//    var $pagindiv = $("#J_trade_pagenation");
//    //new
//    var htmltem1 = "";
//    htmltem1 += '<li class="item prev ' + (_data.page > 1 ? '' : 'prev-disabled') + '"><a class="num ' + (_data.page > 1 ? 'J_MakePoint' : '') + '" href="javascript:void(0);" ' + (_data.page > 1 ? 'onclick="doToPageNm(' + (_data.page - 1) + ');"' : '') + '><span class="icon"></span></a></li>';
//    htmltem1 += '<li class="item active" pagenum="' + _data.page + '">' + _data.page + ' </li>';
//    for (var i = _data.page + 1; i <= 5 && i <= _data.total; i++) {
//        htmltem1 += '<li class="item" pagenum="' + i + '"><a class="num " href="javascript:void(0);" onclick="doToPageNm(' + i + ');">' + i + ' </a></li>';
//    }
//    if (_data.page + 4 < _data.total) {
//        htmltem1 += '<li class="item dot">...</li>';
//    }
//    htmltem1 += '<li class="item next ' + (_data.page < _data.total ? '' : 'next-disabled') + '"><a class="num  ' + (_data.page < _data.total ? 'J_MakePoint' : '') + ' " href="javascript:void(0);" ' + (_data.page < _data.total ? ' onclick="doToPageNm(' + (_data.page + 1) + ');"' : '') + '><span class="icon"></span></a></li>';

//    $pagindiv.find(" ul").html(htmltem1);

//    $pagindiv.find(".total").html("共 " + _data.total + " 页，");
//    $pagindiv.find(".input").attr("max", _data.total);

};

//docancelorder,toreturncharge,dodeleteorder,doresetlogisticamt,dosetdiscountamt
function doaction(saction, soid, slid) {
    if (saction == "dodeleteorder") {
        if (!confirm("你确认要删除订单？删除后将不能被恢复！")) {
            return;
        }
    }
    if (saction == "toreturncharge") {
        if (!confirm("你确认要申请退款并取消订单吗？")) {
            return;
        }
    }
    var _jsParam = new jsonRow();
    _jsParam.AddCell("ACTION", saction);
    _jsParam.AddCell("ID", soid);
    _jsParam.AddCell("LID", slid);
    ajaxComm(_jsParam);
};

function doSellerReply() {
    var _jsParam = new jsonRow();
    _jsParam.AddCell("ACTION", "doSellerReply");
    _jsParam.AddCell("ORDER_LIST_ID", $("#txtORDER_LIST_ID").val());
    _jsParam.AddCell("BUYER_RATE_DESC2", $("#txtSELLER_REPLY1").val());
    ajaxComm(_jsParam);
};
var layerform;
function ShowForm(slid) {
    $("#txtORDER_LIST_ID").val(slid);
    layerform && layer.close(layerform);
    layerform = $.layer({
        type: 1,
        shade: [0],
        fix: true,
        title: '输入回复信息',
        maxmin: false,
        page: { dom: '#J_FormBox' },
        area: ['610px', '410px']
    });
};